﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Jalal.Library.Utility;
using Jalal.Library.DataStructure;
using Jalal.Library.Data;
using BCGC.DAL;
using System.Configuration;

namespace BCGC.BLL.BusinessObject.Resource
{
    public class TrailerFormat
    {
        private int trailerFormatId;
        public int TrailerFormatId
        {
            get { return trailerFormatId; }
            set { trailerFormatId = value; }
        }

        private string videoFormat;
        public string VideoFormat
        {
            get { return videoFormat; }
            set { videoFormat = value; }
        }

        private string fileUrl;
        public string FileUrl
        {
            get { return fileUrl; }
            set { fileUrl = value; }
        }

        private int trailerId;
        public int TrailerId
        {
            get { return trailerId; }
            set { trailerId = value; }
        }

        public TrailerFormat()
        {
            this.trailerFormatId = 0;
            this.videoFormat = string.Empty;
            this.fileUrl = string.Empty;
            this.trailerId = 0;
        }

        public TrailerFormat(DataRow dataRow)
        {
            if (dataRow != null)
            {
                if (dataRow.Table.Columns.Contains("TrailerFormatId"))
                    this.trailerFormatId = (int)CommonUtility.FilterNull(dataRow["TrailerFormatId"], typeof(int));
                if (dataRow.Table.Columns.Contains("TrailerId"))
                    this.trailerId = (int)CommonUtility.FilterNull(dataRow["TrailerId"], typeof(int));
                if (dataRow.Table.Columns.Contains("VideoFormat"))
                    this.videoFormat = (string)CommonUtility.FilterNull(dataRow["VideoFormat"], typeof(string));
                if (dataRow.Table.Columns.Contains("FileUrl"))
                    this.fileUrl = (string)CommonUtility.FilterNull(dataRow["FileUrl"], typeof(string));
            }
            else
            {
                this.trailerFormatId = 0;
                this.videoFormat = string.Empty;
                this.fileUrl = string.Empty;
                this.trailerId = 0;
            }
        }

        private static ReadWriteCustomCollection<TrailerFormat> LoadFromDataTable(DataTable data)
        {
            ReadWriteCustomCollection<TrailerFormat> result = new ReadWriteCustomCollection<TrailerFormat>();

            if (data != null && data.Rows.Count > 0)
            {
                foreach (DataRow row in data.Rows)
                {
                    result.Add(new TrailerFormat(row));
                }
            }
            return result;
        }

        public static ReadWriteCustomCollection<TrailerFormat> GetAllTrailerFormatsByTrailerId(int trailerId)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            ReadWriteCustomCollection<TrailerFormat> trailerFormats = null;
            DataTable data = ResourceData.GetTrailerFormatsByTrailerId(trailerId);
            if (data != null && data.Rows.Count > 0)
                trailerFormats = LoadFromDataTable(data);

            DataFactory.GetDataMiner().CommitTransaction();

            return trailerFormats;
        }

        public static bool Create(int trailerId, string videoFormat, string fileUrl)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            int id = ResourceData.CreateNewTrailerFormat(trailerId, videoFormat, fileUrl);
            DataFactory.GetDataMiner().CommitTransaction();

            return (id > 0);
        }

        public static TrailerFormat GetTrailerFormatByTrailerFormatId(int trailerFormatId)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            TrailerFormat trailerFormat = null;
            DataTable data = ResourceData.GetTrailerFormatById(trailerFormatId);
            if (data != null && data.Rows.Count > 0)
                trailerFormat = new TrailerFormat(data.Rows[0]);

            DataFactory.GetDataMiner().CommitTransaction();

            return trailerFormat;
        }

        public static bool Update(int trailerFormatId, string videoFormat, string fileUrl)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            List<int> trailerFormatsToDelete = new List<int>();
            trailerFormatsToDelete.Add(trailerFormatId);
            DataTable trailerFormatFiles = ResourceData.GetTrailerFormatFileLocationsForDelete(trailerFormatsToDelete);
            DataFactory.GetDataMiner().CommitTransaction();

            if (trailerFormatFiles != null && trailerFormatFiles.Rows.Count > 0)
            {
                foreach (DataRow row in trailerFormatFiles.Rows)
                {
                    string oldFileUrl = (string)row["FileUrl"];
                    if (!string.IsNullOrEmpty(oldFileUrl))
                        FileAccessUtility.DeleteFile(oldFileUrl);
                }
            }

            DataFactory.GetDataMiner().BeginTransaction(true);
            int updateCount = ResourceData.UpdateTrailerFormat(trailerFormatId, videoFormat, fileUrl);
            DataFactory.GetDataMiner().CommitTransaction();

            return (updateCount > 0);
        }

        public static bool AllreadyFormatExists(string videoFormat)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);

            int count = ResourceData.GetTrailerFormatCountByVideoFormat(videoFormat);

            DataFactory.GetDataMiner().CommitTransaction();

            return (count > 0);
        }

        public static bool Delete(List<int> trailerFormatIdsToDelete)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable trailerFormatFiles = ResourceData.GetTrailerFormatFileLocationsForDelete(trailerFormatIdsToDelete);
            DataFactory.GetDataMiner().CommitTransaction();

            DataFactory.GetDataMiner().BeginTransaction(false);

            try
            {
                int deletedItemCount = ResourceData.DeleteTrailerFormats(trailerFormatIdsToDelete);
                if (deletedItemCount == trailerFormatIdsToDelete.Count)
                    DataFactory.GetDataMiner().CommitTransaction();
                else
                    throw new Exception("Failed to delete all items");

                if (trailerFormatFiles != null && trailerFormatFiles.Rows.Count > 0)
                {
                    foreach (DataRow row in trailerFormatFiles.Rows)
                    {
                        string fileUrl = (string)row["FileUrl"];
                        if (!string.IsNullOrEmpty(fileUrl))
                            FileAccessUtility.DeleteFile(fileUrl);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                DataFactory.GetDataMiner().RollBackTransaction();
                throw ex;
            }
        }

        public static string UploadTrailerFormatFile(System.Web.HttpPostedFile postedFile)
        {
            return FileAccessUtility.UploadFile(postedFile, ConfigurationSettings.AppSettings["FCKeditor:UserFilesPath"]
                + "file/trailer/", FileAccessUtility.FileTypes.Zip);
        }
    }
}
